******************************************************************************************************************************************************
*This do file produce figures in "Firm Growth and Corruption: Empirical Evidence from Vietnam"												   																	    *
******************************************************************************************************************************************************
clear all
set more off
set mem 600m
set maxvar 3000


*SET DIRECTORY HERE*
global dir_data_original /Users/Jie/Dropbox (Personal)/CorruptionIncome/EJFinalSubmission/data&program/original raw data/
global dir_data_coded /Users/Jie/Dropbox (Personal)/CorruptionIncome/EJFinalSubmission/data&program/intermediary data/
global dir_data_analysis /Users/Jie/Dropbox (Personal)/CorruptionIncome/EJFinalSubmission/data&program/analysis data/

global dir_output_table /Users/Jie/Dropbox (Personal)/CorruptionIncome/EJFinalSubmission/tables/raw
global dir_output_figure /Users/Jie/Dropbox (Personal)/CorruptionIncome/EJFinalSubmission/figures

cap log close

********** Figure 1 **********
insheet using "${dir_data_analysis}/pwt.csv",delimit(",") clear
rename countryisocode ccodealp
drop if rgdpch=="na"
destring rgdpch,replace
label var rgdpch "PPP Converted GDP Per Capita (Chain Series), at 2005 constant prices"
g lnrgdpch=ln(rgdpch)
sort ccodealp year
tempfile rgdpc_2005price
save `rgdpc_2005price',replace

use `rgdpc_2005price',clear
rename country cname
sort cname year
save `rgdpc_2005price',replace

insheet using "${dir_data_analysis}/WES.csv",clear
drop in 1/8
keep if subgroup==""
rename economy cname
sort cname year
merge 1:1 cname year using `rgdpc_2005price'
drop _merge

rename v6 anycorruption
label var anycorruption "Percent of firms expected to give gifts to public officials to get things done"
drop if anycorruption=="..."
destring anycorruption,replace
tostring year,replace
g year2=substr(year,3,2)
g cyear=ccodealp+year2 if ccodealp!="" & year2!=""

/*Keep the most recent year for each country*/
drop if anycorruption==.
destring year,replace
bys ccodealp: egen recentyear=max(year)
keep if year==recentyear
twoway (lfit anycorruption lnrgdpch) (scatter anycorruption lnrgdpch, msymbol(i) mlabp(0) mlabel(ccodealp) mlabcolor(maroon)), ///
		ytitle("% of firms expected to give gifts to public officials", size(medsmall) margin(medium))  xtitle("Real GDP per Capita (Ln)", size(medsmall) margin(medium)) xlabel(5(2)11) legend(off) ///
		graphregion(color(white)) bgcolor(white)
		
graph export "${dir_output_figure}/Figure1.pdf", replace


********** Figure 2 **********
use "${dir_data_analysis}/WES_micro_allcountries",clear

keep if sample==1

ren j7a informalpay 
replace informalpay=. if informalpay<0
sum informalpay,d

sum size_num,d
replace size_num=. if size_num>r(p99) |size_num<r(p1)

twoway (lpolyci informalpay size_num,bw(100)), ///
		ytitle("% total annual sales paid in informal payments" "(Local polynomial regression on pooled data)", size(medsmall) margin(medium))  xtitle("Total number of full time employees", size(medsmall) margin(medium)) legend(off) ///
		graphregion(color(white)) bgcolor(white)
		
graph export "${dir_output_figure}/Figure2a.pdf", replace	

/*within country*/
egen cid=group(country)

bys cid:egen a=mean(size_num)
bys cid:egen b=mean(informalpay)

g size_res=size_num-a
g informalpay_res=informalpay-b

twoway (lpolyci informalpay size_res,bw(100)), ///
		ytitle("% total annual sales paid in informal payments" "(Local polynomial regression within country)", size(medsmall) margin(medium))  xtitle("Total number of full time employees", size(medsmall) margin(medium)) legend(off) ///
		graphregion(color(white)) bgcolor(white)
		
graph export "${dir_output_figure}/Figure2b.pdf", replace	


********** Figure 3 **********
use "${dir_data_analysis}/PCI_coded.dta",clear

g bribe_pctrev_d=(bribe_pctrev>2&bribe_pctrev!=.)
replace bribe_common=bribe_common*100
collapse (mean) bribe_pctrev_d bribe_pctrev bribe_common, by(pci_id year)

cap graph drop hist_bribecommon_2010 hist_bribeshare_2010

histogram bribe_common if year==2010, name(hist_bribecommon_2010) bin(10) fcolor(maroon) lcolor(white)  ///
	percent xtitle("% of Firm Answering Bribes are Common", size(medsmall) margin(medsmall)) ytitle("Share of Provinces") ///
	graphregion(color(white)) bgcolor(white)

histogram bribe_pctrev_d if year==2010, name(hist_bribeshare_2010) bin(10) fcolor(maroon) lcolor(white)  ///
	percent xtitle("Fraction of Firm with Bribe Payment Greater than 2% of revenue", size(medsmall) margin(medsmall)) ytitle("Share of Provinces") ///
	graphregion(color(white)) bgcolor(white)

graph combine hist_bribecommon_2010 hist_bribeshare_2010 ,  ycommon imargin(vsmall) graphregion(color(white))

graph export "${dir_output_figure}/Figure3.pdf", replace


********** Figure 4 **********
use "${dir_data_analysis}/finalanalysissample",clear

drop if employ_lag==.b & employ==.b
keep if (employ_lag>2 & employ_lag!=.b) | (employ_lag==.b & employ>2 & yrsopen>0 & yrsopen!=.)

egen x=group(bribe_pctrev)

g a=_N
bys x: g b=_N
g freq=b/a

bys x:g tag=_n

graph twoway (bar freq x if tag==1 ,sort fcolor(green) lcolor(black) yaxis(1) ytitle("Fraction of firms", axis(1))), ///
			 xlabel(1 "0 %" 2 "<1%" 3 "1-2%" 4 "2-10%" 5 "10-20%" 6 "20-30%" 7 ">30%") ///
			 xtitle("Bribe as % of revenue") ///
			 graphregion(color(white)) bgcolor(white)

graph export "${dir_output_figure}/Figure4.pdf",replace


********** Figure 5 **********
use "${dir_data_analysis}/finalanalysissample",clear

drop if employ_lag==.b & employ==.b
keep if (employ_lag>2 & employ_lag!=.b) | (employ_lag==.b & employ>2 & yrsopen>0 & yrsopen!=.)

tab employ_n, gen(cat__)
reg bribe_pctrev cat__*, noc 
mat B = e(b)
mat B = B' 
mat SE = e(V) 
mat SE = vecdiag(SE)
mat SE = SE'
cap drop B* SE* b se up down a
svmat B
svmat SE
gen b = B1
gen se = sqrt(SE1)

gen up = b+1.96*se
gen down = b-1.96*se
gen a = _n if _n<7

graph twoway (scatter b a,connect(l) col(blue) lcolor(blue)) ///
	(rcap up down a,lp(dash) lc(blue)), ///
	legend(off) xlabel(1 "10 - 49" 2 "50 - 199" 3 "200 - 299" 4 "300 - 499" 5 "500 - 1000" 6 "> 1000") ///
	xtitle("Employment") ytitle("Bribes as percent of revenue") ///
	graphregion(color(white)) bgcolor(white)

graph export "${dir_output_figure}/Figure5.pdf", replace


